<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Group extends Model
{
    //
    protected $fillable = ['title', 'days', 'site_nums'];
    public function Packages()
    {
        return $this->belongsToMany(Package::class)->withTimestamps();
    }
    public function hasPackage($package)
    {
        return $this->Packages->contains($package);
    }
    //访问器
    public function getModulesAttribute()
    {
        //with(急加载外键)
        return $this->packages()->with('modules')->get()->mapWithKeys(function ($package) {
            return $package->modules;
        });
    }
}
